package com.liuyk.commonlib.utility;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.core.content.FileProvider;

import com.liuyk.commonlib.App;

import java.io.File;
import java.io.IOException;

/**
 * 通用Intent跳转
 * <p>
 * Created by liuyakui on 2020/4/24.
 */
public final class CommonIntentUtils {

    public static void goToPager(String schemeUrl) {
        goToPager(schemeUrl, null);
    }

    public static void goToPager(String schemeUrl, Bundle bundle) {
        goToPager(null, 0, schemeUrl, bundle);
    }

    public static void goToPager(Activity activity, int requestCode, String schemeUrl, Bundle bundle) {
        final Intent intent = new Intent();
        intent.setData(Uri.parse(schemeUrl));
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        if (activity != null) {
            if (activity.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
                activity.startActivityForResult(intent, requestCode);
            }
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            App.context.startActivity(intent);
        }
    }

    /**
     * 安装APK
     *
     * @param context
     * @param apk
     */
    public static void goToInstallApk(Context context, Uri apk) {
        try {
            chmod("777", apk.getPath());
            final Intent intent = new Intent(Intent.ACTION_VIEW);
            //添加这一句表示对目标应用临时授权该Uri所代表的文件
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(getUriForFile(context, apk.getPath()), "application/vnd.android.package-archive");
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件Uri
     *
     * @param context APP上下文
     * @param path    绝对路径
     * @return uri
     */
    public static Uri getUriForFile(Context context, String path) {
        File file = new File(path);
        if (context == null) {
            return null;
        }
        Uri uri = null;
        try {
            //Android7.0以上
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = FileProvider.getUriForFile(context.getApplicationContext(), context.getPackageName() + ".fileprovider", file);
            } else {
                uri = Uri.fromFile(file);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uri;
    }

    /**
     * 获取权限
     *
     * @param permission 权限
     * @param path       路径
     */
    private static void chmod(String permission, String path) {
        try {
            String command = "chmod " + permission + " " + path;
            Runtime runtime = Runtime.getRuntime();
            runtime.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转到权限设置界面
     */
    public static void goToPermissionsSettingPager(Context context) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 21) {
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        }
        context.startActivity(intent);
    }
}
